package cli.view;

// 产生带特效的字符串
public enum TextEffects {
    RESET(0),
    BOLD(1),
    UNDERLINE(4),
    TWINK(5),
    REVERSE(7),
    HIDE(8),

    // 字体颜色
    BLACK(30),
    RED(31),
    GREEN(32),
    YELLOW(33),
    BLUE(34),
    MAGENTA(35),
    CYAN(36),
    WHITE(37),

    // 背景颜色
    BACK_BLACK(40),
    BACK_RED(41),
    BACK_GREEN(42),
    BACK_YELLOW(43),
    BACK_BLUE(44),
    BACK_MAGENTA(45),
    BACK_CYAN(46),
    BACK_WHITE(47);

    // ANSI屏幕控制码
    private final int ANSICode;

    TextEffects(int ANSICode) {
        this.ANSICode = ANSICode;
    }

    // ANSI转义字符
    private static String ANSIEsc(TextEffects effect) {
        return String.format("\033[%dm", effect.ANSICode);
    }

    // 产生带特效的字符串
    public static String apply(String text, TextEffects... effects) {
        StringBuilder result = new StringBuilder();
        for (TextEffects effect : effects) {
            result.append(ANSIEsc(effect));
        }
        result.append(text);
        result.append(ANSIEsc(RESET));
        return result.toString();
    }
}
